<?php

class Index_IndexAction extends C_Action  
{
	private $islogin = 0;
	private $user_E = null;
	public function init()
	{
		$this->islogin = Zend_Registry::get('is_login');
		$this->user_E = Zend_Registry::get('user_E');
	}
	public function doIndex()
	{ 
		$self_site_E = Sites::getByUserid($this->user_E->id);
		$this->_response->self_site_E = $self_site_E;
		
	}
	private function check()
	{
	//检查是否登陆
		if(!$this->islogin)
		{
			$login_url = $this->getUrl("index","auth","token");
			$this->RedirectJump($login_url);
		}
		//检查是否已经开通站点
		$site_E = Sites::getByUserid($this->user_E->id);
		if(!empty($site_E))
		{
			$login_url = $this->getUrl("index","site","index",array("siteid"=>$site_E->id));
			$this->RedirectJump($login_url);
		}
	}
	
	public function doApply()
	{
		$this->check();
		$update = $this->_request->get("update");
		$email = $this->_request->get("email");
		$title = $this->_request->get("title");
		$descript = $this->_request->get("descript");
		$error = $this->_request->get("error");
		$this->_response->email = $email;
		$this->_response->title = $title;
		$this->_response->descript = $descript;
		$this->_response->error = $error;
		
		if($update)
		{
			if(empty($email))
			{
				$error = "E-mail可不能不填啊!";
				$error_url = $this->getUrl("index","index","apply",array("siteid"=>$site_E->id),
				array("email"=>$email,"title"=>$title,"descript"=>$descript,"error"=>$error));
				$this->RedirectJump($error_url);
			}
			if(!preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',
						$email))
			{
				$error = "我敢说没这种e-mail地址!";
				$error_url = $this->getUrl("index","index","apply",array("siteid"=>$site_E->id),
				array("email"=>$email,"title"=>$title,"descript"=>$descript,"error"=>$error));
				$this->RedirectJump($error_url);			
			}
			$user_E = Users::getByEmail($email);
			if(!empty($user_E))
			{
				$error = "此e-mail已经开通小站了!";
				$error_url = $this->getUrl("index","index","apply",array("siteid"=>$site_E->id),
				array("email"=>$email,"title"=>$title,"descript"=>$descript,"error"=>$error));
				$this->RedirectJump($error_url);	
			}
			if(empty($title))
			{
				$error = "小站标题是非填不可的!";
					$error_url = $this->getUrl("index","index","apply",array("siteid"=>$site_E->id),
					array("email"=>$email,"title"=>$title,"descript"=>$descript,"error"=>$error));
					$this->RedirectJump($error_url);
			}
			if(empty($descript))
			{
				$error = "小站描述写点吧不差时间!";
				$error_url = $this->getUrl("index","index","apply",array("siteid"=>$site_E->id),
				array("email"=>$email,"title"=>$title,"descript"=>$descript,"error"=>$error));
				$this->RedirectJump($error_url);
			}
			//创建站点
			$row['title'] = $title;
			$row['descript'] = $descript;
			$row['userid'] = $this->user_E->id;
			$row['sign'] = 1; // 第一次
			$site_E = Sites::insertEntity($row);
			//创建房间
			$info['name'] = '未命名房间';
			$info['userid'] = $this->user_E->id;
			$info['siteid'] = $site_E->id;
			Rooms::insertEntity($info);
			$site_url = $this->getUrl("index","site","index",array("siteid"=>$site_E->id));
			$this->_response->setRedirect($site_url);
			
			$host = $this->_request->getHost();
			$url = "http://$host";
			$site_url = $url."/site/".$site_E->id;
			
			$content = "我@{$this->user_E->getName()} 开通了小站《{$site_E->title}》{$site_E->descript} $site_url ";
			Weibos::update($content);	
			
		}
		
		
	}
	public function doLike()
	{
		if(!$this->islogin)
		{
			$login_url = $this->getUrl("index","auth","token");
			$this->RedirectJump($login_url);
		}
		$siteid = $this->_request->get("siteid");
		$islike = $this->_request->get("islike");
		$site_E = Sites::getById($siteid);
		if(empty($site_E))exit;
		$id = $site_E->mapUser->id;
		
		
		
		
		if($islike)
		{
			
			
			$host = $this->_request->getHost();
			$url = "http://$host";
			$site_url = $url."/site/".$site_E->id."?".rand();
			$logo = $site_E->logo;
			if(empty($logo))
				$pic = $url."/pics/site/icon_default_large.png";
			else 
				$pic = $url.$logo;
			if($id == $this->user_E->id)
			{
				$content = "我@{$this->user_E->getName()} 开通了小站《{$site_E->title}》{$site_E->descript} $site_url ";
				Weibos::upload($content, $pic);	
			}
			
			else if(!Weibos::isFollow($id))
			{
				Weibos::Follow($id);
				$content = "我@{$this->user_E->getName()} 关注了你@{$site_E->mapUser->getName()} 的小站《{$site_E->title}》{$site_E->descript} $site_url ";
				Weibos::upload($content, $pic);
			}
		}
		else
		{
			if(Weibos::isFollow($id))
			{
				
				Weibos::unFollow($id);
				
			}
		}
		exit;
	}
}

?>